<?php


namespace app\system\controller;

class Api extends AdminBase
{
	public function getlist()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\ApiService::getlist($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function getinfo($id)
	{
		$options = ["id" => $id];
		$result = \app\system\service\ApiService::getinfo($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function add()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["name" => "require", "type" => "require", "controller" => "require", "method" => "require", "api" => "require", "describe" => "require", "verify_user" => "require"], ["name.require" => "name 不能为空", "type.require" => "type 不能为空", "controller.require" => "controller 不能为空", "method.require" => "method 不能为空", "api.require" => "api 不能为空", "describe.require" => "describe 不能为空", "verify_user.require" => "verify_user 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\ApiService::add($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["name" => "require", "type" => "require", "controller" => "require", "method" => "require", "api" => "require", "describe" => "require", "verify_user" => "require"], ["name.require" => "name 不能为空", "type.require" => "type 不能为空", "controller.require" => "controller 不能为空", "method.require" => "method 不能为空", "api.require" => "api 不能为空", "describe.require" => "describe 不能为空", "verify_user.require" => "verify_user 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\ApiService::edit($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "编辑成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del()
	{
		$options = $this->request->post();
		$result = \app\system\service\ApiService::del($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_app_config($api_id, $appid)
	{
		$options = ["api_id" => $api_id, "appid" => $appid];
		$result = \app\system\service\ApiService::get_app_config($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function set_app_config()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["api_id" => "require", "appid" => "require"], ["api_id.require" => "api_id 不能为空", "appid.require" => "appid 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\ApiService::set_app_config($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "修改成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function update_app_api_config_route_cache()
	{
		$result = \app\system\service\ApiService::update_app_api_config_route_cache();
		if ($result["code"] == 1) {
			$this->response($result["code"], "更新成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
}